//
//  JargonViewController.m
//  UEAssistant
//
//  Created by mac on 2017/8/2.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "JargonViewController.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>
@interface JargonViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>{
    
    NSMutableArray                  *_dataSource;
    NSMutableArray                  *_userIdArr;
}

@property (nonatomic, strong) UILabel               *NaviTitle;
@property (nonatomic, strong) UITableView           *tableView;
@property (nonatomic, strong) UILabel               *useLabel;
@property (nonatomic, strong) UILabel               *update;
@property (nonatomic, strong) UIButton              *selectBt;
@property (nonatomic, strong) UIButton              *selectBt_update;

@end

@implementation JargonViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [self.NaviTitle setHidden:NO];
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [self.NaviTitle setHidden:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self LoadData];
    [self layoutUI];
}

- (void)layoutUI{
    
    self.view.backgroundColor = RGB(247, 247, 247, 1);
    [self.navigationController.navigationBar addSubview:self.NaviTitle];
    [self.view addSubview:self.tableView];
}

- (void)LoadData{
    
    _userIdArr = [NSMutableArray array];
    
    [LSProgressHUD showOnWindow];
    NSDictionary *params = @{@"projectId":shareData.product_Id,
                             @"corpusType":@"tbx",@"page":@"1"};
    [AvailableNetWorking postWithUrl:checkCorpus_connector params:params successHandler:^(id responseObject) {
        
        NSLog(@"SHUYUKUdata == %@",responseObject);
        if ([responseObject[@"code"] integerValue] == 200) {
            _dataSource = [NSMutableArray arrayWithArray:responseObject[@"result"]];
            [self.tableView reloadData];
            [LSProgressHUD hideForWindow];
        }
    } falureHandler:^(NSError *error) {
        
    }];
}

- (void)refreshShareListdataMs{

    NSDictionary *params = @{@"projectId":shareData.product_Id,
                             @"corpusType":@"tbx",@"page":@"1"};
    [AvailableNetWorking postWithUrl:checkCorpus_connector params:params successHandler:^(id responseObject) {
        
        NSLog(@"SHUYUKUdata == %@",responseObject);
        if ([responseObject[@"code"] integerValue] == 200) {
            _dataSource = [NSMutableArray arrayWithArray:responseObject[@"result"]];
            [self.tableView reloadData];
            self.view.userInteractionEnabled = YES;
            [TopAlertView SetUpbackgroundColor:mainColor andStayTime:2.0 andImageName:@"imagePlaceholder" andTitleStr:@"操作成功" andTitleColor:[UIColor whiteColor] andCancel:NO];

        }
    } falureHandler:^(NSError *error) {
        
    }];

}

#pragma mark -- <UITableViewDelegate,UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    cell.textLabel.text = _dataSource[indexPath.row][@"tbx_dbname"];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ -> %@",_dataSource[indexPath.row][@"sourcelangname"],_dataSource[indexPath.row][@"targetlangname"]];
    cell.textLabel.textColor = RGB(51, 51, 51, 1);
    cell.detailTextLabel.textColor = RGB(153, 153, 153, 1);
    cell.textLabel.font = [UIFont systemFontOfSize:FLEXIBLE(15)];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:FLEXIBLE(12)];
    
    _useLabel = [[UILabel alloc]initWithFrame:CGRectMake(Screen_Width-160*Radio, 20*Radio, 30*Radio, 20*Radio)];
    _useLabel.backgroundColor = [UIColor clearColor];
    _useLabel.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
    _useLabel.textColor = RGB(143, 143, 143, 1);
    _useLabel.text = @"使用";
    [cell addSubview:_useLabel];
    
    _selectBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectBt.frame = CGRectMake(Screen_Width-130*Radio, 20*Radio, 20, 20);
    if ([[NSString stringWithFormat:@"%@",_dataSource[indexPath.row][@"isUse"]] isEqualToString:@"1"]) {
        
        [_selectBt setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];
        
        _update = [[UILabel alloc]initWithFrame:CGRectMake(Screen_Width-80*Radio, 20*Radio, 30*Radio, 20*Radio)];
        _update.backgroundColor = [UIColor clearColor];
        _update.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
        _update.textColor = RGB(143, 143, 143, 1);
        _update.text = @"QA";
        [cell addSubview:_update];
        
        _selectBt_update = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectBt_update.frame = CGRectMake(Screen_Width-50*Radio, 20*Radio, 20, 20);
        if ([[NSString stringWithFormat:@"%@",_dataSource[indexPath.row][@"isUpdateId"]] isEqualToString:@"1"]) {
            [_selectBt_update setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];
        }else{
            [_selectBt_update setImage:[UIImage imageNamed:@"选择"] forState:UIControlStateNormal];
        }
        [_selectBt_update addTarget:self action:@selector(selectToolsClied_update:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:_selectBt_update];
        
    }else{
        [_selectBt setImage:[UIImage imageNamed:@"选择"] forState:UIControlStateNormal];
    }
    [_selectBt addTarget:self action:@selector(selectToolsClied_use:) forControlEvents:UIControlEventTouchUpInside];
    _selectBt.tag = 100 + indexPath.row;
    [cell addSubview:_selectBt];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}


#pragma mark -- Event

- (void)selectToolsClied_use:(UIButton *)sender{
    
    self.view.userInteractionEnabled = NO;
    UITableViewCell *cell = (UITableViewCell *)sender.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [_userIdArr removeAllObjects];
    __block NSString *updateID;

    if ([[NSString stringWithFormat:@"%@",_dataSource[indexPath.row][@"isUse"]] isEqualToString:@"1"]) {
        
        [_dataSource enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ([[NSString stringWithFormat:@"%@",obj[@"isUse"]] isEqualToString:@"1"]) {
                [_userIdArr addObject:obj[@"tbx_dbid"]];
            }
            if ([[NSString stringWithFormat:@"%@",obj[@"isUpdateId"]] isEqualToString:@"1"]) {
                updateID = [NSString stringWithFormat:@"%@",obj[@"tbx_dbid"]];
            }
            
        }];
        [_userIdArr removeObject:_dataSource[indexPath.row][@"tbx_dbid"]];
        NSString *IdString = [_userIdArr componentsJoinedByString:@","];
        NSDictionary *params = @{@"projectId":shareData.product_Id,
                                 @"idList":IdString,
                                 @"updatecorpusId":updateID,
                                 @"corpusType":@"tbx"};
        
        [AvailableNetWorking postWithUrl:savelangKitInfo_connector params:params successHandler:^(id responseObject) {
            
            NSLog(@"rrsssss == %@",responseObject);
            [self refreshShareListdataMs];
            
        } falureHandler:^(NSError *error) {
            
        }];
        
    }
    else{
        [_dataSource enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ([[NSString stringWithFormat:@"%@",obj[@"isUse"]] isEqualToString:@"1"]) {
                [_userIdArr addObject:obj[@"tbx_dbid"]];
            }
            if ([[NSString stringWithFormat:@"%@",obj[@"isUpdateId"]] isEqualToString:@"1"]) {
                updateID = [NSString stringWithFormat:@"%@",obj[@"tbx_dbid"]];
            }
        }];
        [_userIdArr addObject:_dataSource[indexPath.row][@"tbx_dbid"]];
        NSString *IdString = [_userIdArr componentsJoinedByString:@","];
        NSDictionary *params = @{@"projectId":shareData.product_Id,
                                 @"idList":IdString,
                                 @"updatecorpusId":updateID,
                                 @"corpusType":@"tbx"};
        
        [AvailableNetWorking postWithUrl:savelangKitInfo_connector params:params successHandler:^(id responseObject) {
            
            NSLog(@"rrsssss == %@",responseObject);
            [self refreshShareListdataMs];
            
        } falureHandler:^(NSError *error) {
            
        }];
        
    }

}

- (void)selectToolsClied_update:(UIButton *)sender{
    
    UITableViewCell *cell = (UITableViewCell *)sender.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [_userIdArr removeAllObjects];
    
    if (![[NSString stringWithFormat:@"%@",_dataSource[indexPath.row][@"isUpdateId"]] isEqualToString:@"1"]) {
        
        [_dataSource enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ([[NSString stringWithFormat:@"%@",obj[@"isUse"]] isEqualToString:@"1"]) {
                [_userIdArr addObject:obj[@"tbx_dbid"]];
            }
        }];
        NSString *IdString = [_userIdArr componentsJoinedByString:@","];
        NSDictionary *params = @{@"projectId":shareData.product_Id,
                                 @"idList":IdString,
                                 @"updatecorpusId":_dataSource[indexPath.row][@"tbx_dbid"],
                                 @"corpusType":@"tbx"};
        
        [AvailableNetWorking postWithUrl:savelangKitInfo_connector params:params successHandler:^(id responseObject) {
            
            NSLog(@"rrs == %@",responseObject);
            [self refreshShareListdataMs];
            
        } falureHandler:^(NSError *error) {
            
        }];
        
        
    }
    else{
        [_dataSource enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ([[NSString stringWithFormat:@"%@",obj[@"isUse"]] isEqualToString:@"1"]) {
                [_userIdArr addObject:obj[@"tbx_dbid"]];
            }
        }];
        NSString *IdString = [_userIdArr componentsJoinedByString:@","];
        NSDictionary *params = @{@"projectId":shareData.product_Id,
                                 @"idList":IdString,
                                 @"updatecorpusId":@"",
                                 @"corpusType":@"tbx"};
        
        [AvailableNetWorking postWithUrl:savelangKitInfo_connector params:params successHandler:^(id responseObject) {
            
            NSLog(@"rrs == %@",responseObject);
            [self refreshShareListdataMs];
            
        } falureHandler:^(NSError *error) {
            
        }];
        
    }
    
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"暂无相关设置" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#666666"]}];
    return str;
}
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
                                   
    return [UIImage imageNamed:@"任务"];
}
#pragma mark -- getter
- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = RGB(247, 247, 247, 1);
        _tableView.rowHeight = 60*Radio;
        _tableView.tableFooterView = [UIView new];
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
    }
    return _tableView;
}

- (UILabel *)NaviTitle{
    
    if (!_NaviTitle) {
        _NaviTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
        _NaviTitle.center = CGPointMake(self.view.center.x, 20);
        _NaviTitle.backgroundColor = [UIColor clearColor];
        _NaviTitle.text = @"术语库设置";
        _NaviTitle.textAlignment = NSTextAlignmentCenter;
        _NaviTitle.textColor = [UIColor colorWithHexString:@"#333333"];
    }
    return _NaviTitle;
}


@end
